package iit.projects.etaxi.view.converters;

import iit.projects.etaxi.util.Constants;
import iit.projects.etaxi.util.JSFUtils;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;


/**
 * <p>This converter extends NumberConverter and sets a generic message for any conversion errors.</p>
 * <p>The specific message will be set as the detail message.</p>
 */
public class GenericNumberConverter extends NumberConverter {

	public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String string) {
		try {
			return super.getAsObject(facesContext, uiComponent, string);
		} catch (ConverterException e) {
			String message = JSFUtils.getStringFromBundle(Constants.BundleNames.MESSAGES, "common.conversion.invalidValue", new Object[] {string});
			FacesMessage facesMessage = e.getFacesMessage();
			facesMessage.setDetail(message);
			throw e;
		}
	}
}
